<template>

  <router-view v-if="isRouterAlive"/>


</template>

<script setup>

import {nextTick, onBeforeMount, provide, ref} from "vue";
import {useStore} from "vuex";

const store=useStore()


onBeforeMount( () => {
  // 在页面加载时读取sessionStorage里的状态信息
    if (sessionStorage.getItem("blog-store")) {
      store.replaceState(Object.assign({}, store.state, JSON.parse(sessionStorage.getItem("blog-store"))))
      sessionStorage.removeItem("blog-store");
    }

    //在页面刷新时将vuex里的信息保存到sessionStorage里
    window.addEventListener("beforeunload", () => {
      sessionStorage.setItem("blog-store", JSON.stringify(store.state))
    })
})

const isRouterAlive = ref(true);
const reload = () => {
  setTimeout(()=>{
    isRouterAlive.value = false;
    nextTick(() => {
      isRouterAlive.value = true;
    });
  },1000)

};
provide("reload", reload);


// export default {
//   created() {
//     //在页面加载时读取sessionStorage里的状态信息
//     if (sessionStorage.getItem("blog-store")) {
//       this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(sessionStorage.getItem("blog-store"))))
//       sessionStorage.removeItem("blog-store");
//     }
//
//     //在页面刷新时将vuex里的信息保存到sessionStorage里
//     window.addEventListener("beforeunload", () => {
//       sessionStorage.setItem("blog-store", JSON.stringify(this.$store.state))
//     })
//   }
// }
</script>

